#######################################################################################################
#######################################################################################################
#
##	 	Script para monitorar o ramdisk do squid
###      	Criado por Graziele Borges de Souza (gbsouza@uoldiveo.com)
####		27/07/2011 - 17/08/2011
#####		24/10/2011 - 03/11/2011 [Adicionado case --log]
######		29/12/2011 - Ajustado case --log e adicionada verificação da estrutura de cache
#######		formata_ramdisk.sh
########
#######################################################################################################
#######################################################################################################

# Verifica se o arquivo NÃO existe

if [ ! -e /tmp/watch_ramdisk ]; then
        echo "100" > /tmp/watch_ramdisk
fi

# Declara as variáis

USAGE=`df -h /export/cache/ramdisk | grep ramdisk | awk {'print $5'} | cut -d % -f 1`
VALOR=`cat /tmp/watch_ramdisk`
VERIFICA_SQUID=`ps auxwww | grep -ic /opt/squid/etc/squid-1.conf | grep -v grep`
VERIFICA_MOUNT=`df -h | grep -ic ramdisk`
LIMITE=100
DATA=`date`
DATA_LOG=`date "+%Y/%m/%d %H:%M" | cut -c0-15`
GREP_1=`grep "$DATA_LOG" /export/logs/squid/squid_cache-1.log | grep -ic "No space left on device"`

#Função de formatar e refazer o cache do squid

Formata () {

        /etc/init.d/squid stop

        i=0
        while [ $VERIFICA_SQUID -eq 2 ]; do
                if [ $i -eq 10 ]; then
                        killall -9 squid
                fi
                sleep 5
                VERIFICA_SQUID=`ps auxwww | grep -ic /opt/squid/etc/squid-1.conf | grep -v grep`
                let i++
        done

        umount /export/cache/ramdisk

        i=0
        while [ $VERIFICA_MOUNT -eq 1 ]; do
                if [ $i -eq 10 ]; then
                        fuser -k /export/cache/ramdisk
                        umount -f /export/cache/ramdisk
                fi
                sleep 2
                VERIFICA_MOUNT=`df -h | grep -ic ramdisk`
                let i++
        done

        /etc/init.d/ramdisk

        /opt/squid/sbin/squid -z -f /opt/squid/etc/squid-1.conf

        /etc/init.d/squid start

        echo $USAGE > /tmp/watch_ramdisk
}

Verifica_cache () {

	FIND_DIRS=`find /export/cache/ramdisk -maxdepth 1 -type d | wc -l`

	if [ $FIND_DIRS -ne 34 ]; then
		if [ $VERIFICA_SQUID -eq 2]; then

		        /etc/init.d/squid stop

		        i=0
		        while [ $VERIFICA_SQUID -eq 2 ]; do
		                if [ $i -eq 10 ]; then
					killall -9 squid
				fi
	                	sleep 5
	        	        VERIFICA_SQUID=`ps auxwww | grep -ic /opt/squid/etc/squid-1.conf | grep -v grep`
	                	let i++
			done

			/opt/squid/sbin/squid -z -f /opt/squid/etc/squid-1.conf	

			/etc/init.d/squid start

			echo $USAGE > /tmp/watch_ramdisk

		fi
	fi

}

#Corpo do script com as validaçs

case $1 in

        --cron)

                if [ $VALOR -eq $LIMITE ]; then
                        if [ $USAGE -eq $LIMITE ]; then

                                Formata

				Verifica_cache
								
				echo "Executei como $1 em $DATA" >> /export/logs/formata_ramdisk.log

                        fi
				
				echo $USAGE > /tmp/watch_ramdisk
			
		else								
					
			echo $USAGE > /tmp/watch_ramdisk

               	fi
			
                ;;

	--log)

		if [ $GREP_1 -ge 1 ]; then

                	Formata

			Verifica_cache

			echo $USAGE > /tmp/watch_ramdisk

			echo "Executei como $1 em $DATA apresentando o erro No space left on device" >> /export/logs/formata_ramdisk.log

		fi

		;;	

        --force)

                Formata

		Verifica_cache

                echo "Executei como $1 em $DATA" >> /export/logs/formata_ramdisk.log

                ;;

        *)
                echo "Utilização: $0  [--cron | --force | --log]"
                echo "$0 --cron: Executa utilizando os parametros de monitoraç."
                echo "$0 --force: Efetua a formataç do cache e rebuild independente do status do ramdisk."
		echo "$0 --log: Verifica no log do squid se ha erros, se sim formata."
                ;;
esac
